package com.maket.jbxx.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.core.net.server.Client;
import com.maket.jbxx.domain.CpAndSx;
import com.maket.jbxx.domain.CpppProductBrand;
import com.maket.jbxx.domain.SxAttributeValue;
import com.maket.jbxx.domain.vo.CpProductType;
import com.maket.jbxx.service.ICpAndSxService;
import com.maket.jbxx.service.ISxAttributeValueService;
import com.maket.jbxx.service.impl.CpAndSxServiceImpl;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.maket.jbxx.domain.CpProduct;
import com.maket.jbxx.service.ICpProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 产品管理Controller
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/product")
public class CpProductController extends BaseController
{
    @Autowired
    private ICpProductService cpProductService;

    @Resource
    private CodeClient codeClient;

    @Autowired
    private ICpAndSxService cpAndSxService;

    @Autowired
    private ISxAttributeValueService sxAttributeValueService;

    /**
     * 查询产品管理列表
     */
    @RequiresPermissions("jbxx:product:list")
    @GetMapping("/list")
    public TableDataInfo list(CpProduct cpProduct)
    {
        startPage();
        List<CpProduct> list = cpProductService.selectCpProductList(cpProduct);
        return getDataTable(list);
    }


    /**
     * 查询产品分类修改属性值
     *
     * @param cpProduct 产品管理
     * @return 产品管理集合
     */
    @RequiresPermissions("jbxx:product:list")
    @GetMapping("/type")
    public TableDataInfo selectCpProductType(CpProduct cpProduct){
        List<CpProduct> list = cpProductService.selectCpProductType(cpProduct);
        return getDataTable(list);
    }


    /**
     * 查询产品分类新增属性值
     *
     * @param cpProduct 产品管理
     * @return 产品管理集合
     */
    @RequiresPermissions("jbxx:product:list")
    @GetMapping("/cpfl")
    public TableDataInfo selectCpProductTypeCPFL(CpProduct cpProduct){
        List<CpProduct> list = cpProductService.selectCpProductTypeCode(cpProduct);
        return getDataTable(list);
    }


    /**
     * 查询属性值
     */
    @RequiresPermissions("jbxx:product:list")
    @GetMapping("/list/SxAttribute")
    public TableDataInfo listAttribute(SxAttributeValue sxAttributeValue)
    {
        startPage();
        List<SxAttributeValue> list = sxAttributeValueService.selectSxAttributeValueList(sxAttributeValue);
        return getDataTable(list);
    }

    /**
     * 导出产品管理列表
     */
    @RequiresPermissions("jbxx:product:export")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CpProduct cpProduct)
    {
        List<CpProduct> list = cpProductService.selectCpProductList(cpProduct);
        ExcelUtil<CpProduct> util = new ExcelUtil<CpProduct>(CpProduct.class);
        util.exportExcel(response, list, "产品管理数据");
    }

    /**
     * 获取产品管理详细信息
     */
    @RequiresPermissions("jbxx:product:query")
    @GetMapping(value = "/{cpCode}")
    public AjaxResult getInfo(@PathVariable("cpCode") String cpCode)
    {
        return success(cpProductService.selectCpProductByCpCode(cpCode));
    }

    /**
     * 新增产品管理
     */
    @RequiresPermissions("jbxx:product:add")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpProduct cpProduct)
    {
        cpProduct.setCpCode(codeClient.getCode(3));

        cpProduct.setCpCreate(SecurityUtils.getUsername());
        cpProduct.setCpCreatetime(new Date());

        return toAjax(cpProductService.insertCpProduct(cpProduct));
    }

    /**
     * 修改产品管理
     */
    @RequiresPermissions("jbxx:product:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpProduct cpProduct)
    {
        cpProduct.setCpCreate(SecurityUtils.getUsername());
        cpProduct.setCpUpdatetime(new Date());

        return toAjax(cpProductService.updateCpProduct(cpProduct));
    }


    /**
     * 修改产品状态
     */
    @RequiresPermissions("jbxx:merchants:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping("isStatus")
    public AjaxResult editIsStart(@RequestBody CpProduct cpProduct)
    {
        return toAjax(cpProductService.updateCpppProductBrandIsStatus(cpProduct));
    }


    /**
     * 删除产品管理
     */
    @RequiresPermissions("jbxx:product:remove")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cpCodes}")
    public AjaxResult remove(@PathVariable String[] cpCodes)
    {
        return toAjax(cpProductService.deleteCpProductByCpCodes(cpCodes));
    }
}
